{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 准备百度key和secret\n",
    "API_KEY = '6IPzBqnakoLe5Fmy7S4qEwVq'\n",
    "SECRET_KEY = 'gao6sL8ciVQDlWdK2RRGwTfOprI3IVEr'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'refresh_token': '25.8d24897103bf65b045fc35c3f3b4a5ad.315360000.1972677160.282335-25954485', 'expires_in': 2592000, 'session_key': '9mzdCrPmss4Umio3He0bBsKIzmDbxRcYJT+y+5byyBG6xdauxHvhrVaorHK2VlGILJhqtO/9/KgD0X9GhiV25Lu4s31bCw==', 'access_token': '24.176089999f8aa36070c276740787f6e4.2592000.1659909160.282335-25954485', 'scope': 'public vis-ocr_ocr brain_ocr_scope brain_ocr_general brain_ocr_general_basic vis-ocr_business_license brain_ocr_webimage brain_all_scope brain_ocr_idcard brain_ocr_driving_license brain_ocr_vehicle_license vis-ocr_plate_number brain_solution brain_ocr_plate_number brain_ocr_accurate brain_ocr_accurate_basic brain_ocr_receipt brain_ocr_business_license brain_solution_iocr brain_qrcode brain_ocr_handwriting brain_form brain_ocr_passport brain_ocr_vat_invoice brain_numbers brain_ocr_business_card brain_ocr_train_ticket brain_ocr_taxi_receipt vis-ocr_household_register vis-ocr_vis-classify_birth_certificate vis-ocr_台湾通行证 vis-ocr_港澳通行证 vis-ocr_机动车购车发票识别 vis-ocr_机动车检验合格证识别 vis-ocr_车辆vin码识别 vis-ocr_定额发票识别 vis-ocr_保单识别 vis-ocr_机打发票识别 vis-ocr_行程单识别 brain_ocr_vin brain_ocr_quota_invoice brain_ocr_birth_certificate brain_ocr_household_register brain_ocr_HK_Macau_pass brain_ocr_taiwan_pass brain_ocr_vehicle_invoice brain_ocr_vehicle_certificate brain_ocr_air_ticket brain_ocr_invoice brain_ocr_insurance_doc brain_formula brain_seal brain_ocr_facade brain_ocr_meter brain_doc_analysis brain_ocr_webimage_loc brain_ocr_medical_paper brain_ocr_doc_analysis_office brain_vat_invoice_verification brain_ocr_medical_detail brain_vehicle_registration_certificate brain_ocr_online_taxi_itinerary brain_ocr_multi_idcard brain_ocr_mixed_multi_vehicle brain_ocr_weigth_note brain_ocr_ multiple_invoice wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online smartapp_gov_aladin_to_xcx', 'session_secret': '4aaa3d3ae098d2e1dd2d7d3e652c1285'}\n"
     ]
    }
   ],
   "source": [
    "# 获取token\n",
    "import requests \n",
    "host = 'https://aip.baidubce.com/oauth/2.0/token?'\n",
    "payload = {\n",
    "    'grant_type':'client_credentials',\n",
    "    'client_id':'6IPzBqnakoLe5Fmy7S4qEwVq',\n",
    "    'client_secret':'gao6sL8ciVQDlWdK2RRGwTfOprI3IVEr'\n",
    "}\n",
    "response = requests.get(host,params=payload)\n",
    "if response:\n",
    "    print(response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'24.176089999f8aa36070c276740787f6e4.2592000.1659909160.282335-25954485'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "access_token = response.json()['access_token']\n",
    "access_token"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 驾驶证测试代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'words_result': {'姓名': {'words': '周曙光'}, '至': {'words': '20220722'}, '证号': {'words': '430124198009060433'}, '出生日期': {'words': '19800906'}, '住址': {'words': '湖南省宁乡县煤炭坝镇东风路12号21栋2门11号'}, '发证单位': {'words': '湖南省长沙市公安局交通警察支队'}, '国籍': {'words': '中国'}, '初次领证日期': {'words': '20160722'}, '准驾车型': {'words': 'C1'}, '性别': {'words': '男'}, '有效期限': {'words': '20160722'}}, 'words_result_num': 11, 'log_id': 1545526317571962158}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "驾驶证识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/driving_license\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('F:/桌面/API/期末项目/images/驾驶证.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = '24.b42cc948be48a9df8081d7a6a668dd21.2592000.1659907106.282335-25954485'\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 车牌识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'words_result': {'number': '苏E82L84', 'vertexes_location': [{'x': 271, 'y': 294}, {'x': 925, 'y': 379}, {'x': 901, 'y': 628}, {'x': 244, 'y': 546}], 'color': 'blue', 'probability': [0.9998853207, 0.999958992, 0.9998971224, 0.9999201298, 0.9999659061, 0.999589026, 0.9997789264, 0.999856472]}, 'log_id': 1545526329047282367}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "车牌识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/license_plate\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('F:/桌面/API/期末项目/images/车牌.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = '24.b42cc948be48a9df8081d7a6a668dd21.2592000.1659907106.282335-25954485'\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 行驶证识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'words_result': {'车辆识别代号': {'words': 'LGBGG22E6HY041221'}, '住址': {'words': '山东省滕州市龙阳镇'}, '发证单位': {'words': '山东省枣庄市公安局交通巡逻警察支队'}, '车辆类型': {'words': '小型轿车'}, '品牌型号': {'words': '东风日产牌DFL7165VBL6'}, '发证日期': {'words': '20170204'}, '所有人': {'words': '陈宁'}, '号牌号码': {'words': '鲁D28223'}, '使用性质': {'words': '非营运'}, '发动机号码': {'words': '3389Y55'}, '注册日期': {'words': '20170204'}}, 'words_result_num': 11, 'log_id': 1545526341352300865}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "行驶证识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/vehicle_license\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('F:/桌面/API/期末项目/images/行驶证.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = '24.b42cc948be48a9df8081d7a6a668dd21.2592000.1659907106.282335-25954485'\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 车辆合格证识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'words_result_num': 28, 'direction': 0, 'words_result': {'CarModel': 'CC6460RM01', 'CarBrand': '长城牌', 'CertificateDate': '2013年01月11日', 'CarColor': '灰', 'Displacement': '1497', 'Power': '110', 'Manufacturer': '长城汽车股份有限公司', 'ManufactureDate': '2013年01月11日', 'EngineNo': '1305508358', 'FuelType': '汽油', 'EmissionStandard': 'GB18352.3-2005国IV', 'SteeringType': '方向盘', 'LimitPassenger': '', 'EngineType': 'GW4G15B', 'TyreNum': '4', 'SpeedLimit': '180', 'TotalWeight': '2051', 'SaddleMass': '1541', 'VinNo': 'LGWEF4A50DF159780', 'Wheelbase': '2680', 'AxleNum': '2', 'CertificationNo': 'WAN026130203422', 'CarName': '旅行车', 'ChassisModel': '', 'ChassisID': '', 'SeatingCapacity': '5', 'QualifySeal': '1', 'CGSSeal': '0'}, 'log_id': 1545527025603111052}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "车辆合格证识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/vehicle_certificate\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('F:/桌面/API/期末项目/images/车辆合格证.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = '24.b42cc948be48a9df8081d7a6a668dd21.2592000.1659907106.282335-25954485'\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
